using System;
using System.Collections.Generic;

namespace Bordecal.ImportsSorter
{
	/// <summary>
	/// Comparer for ImportsDirective instances.
	/// </summary>
	internal sealed class ImportsDirectiveComparer : IComparer<ImportsDirective>
	{
		#region MyRegion
		
		/// <summary>
		/// Initializes a new instance of the ImportsDirectiveComparer class.
		/// </summary>
		internal ImportsDirectiveComparer()
		{
		}

		#endregion

		#region IComparer<ImportsDirective> Members

		/// <summary>
		/// Compares two ImportsStatement instances.
		/// </summary>
		/// <param name="x">The first instance to compare.</param>
		/// <param name="y">The second instance to compare.</param>
		/// <returns>
		/// 	<list type="table">
		///			<listheader>
		///				<term>Value</term>
		///				<description>Condition</description>
		///			</listheader>
		///			<item>
		///				<term>Less than zero</term>
		///				<description>This first instance has a lower sorting group index or namespace than the second instance.</description>
		///			</item>
		///			<item>
		///				<term>Zero</term>
		///				<description>The instances have the same sorting group and namespace.</description>
		///			</item>
		///			<item>
		///				<term>Greater than zero</term>
		///				<description>This first instance has a higher sorting group index or namespace than the second instance.</description>
		///			</item>
		///		</list>
		/// </returns>
		public int Compare(ImportsDirective x, ImportsDirective y)
		{
			int retVal;

			if (object.ReferenceEquals(x, y))
			{
				retVal = 0;
			}
			else if (x == null)
			{
				retVal = -1;
			}
			else if (y == null)
			{
				retVal = 1;
			}
			else
			{
				retVal = Comparer<int>.Default.Compare(x.SortingGroup, y.SortingGroup);

				if (retVal == 0)
				{
					retVal = string.Compare(x.NameSpace, y.NameSpace, StringComparison.OrdinalIgnoreCase);
				}
			}

			return retVal;
		}

		#endregion
	}
}